package com.mca.framework.completableFuture;

import java.util.concurrent.*;

/**
 * @Author: zhoupeng
 * @createTime: 2025年07月07日 21:45:19
 * @version: 1.0
 * @Description:
 * @copyright:
 */
public class ApplyToEitherDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture future1 = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future1："+Thread.currentThread().getName());
            return "hello";
        },executorService);

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()-> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future2："+Thread.currentThread().getName());
            return "itheima";
        },executorService);

        //acceptEitherAsync  applyToEitherAsync 那个线程先执行完，并获取返回的结果
        CompletableFuture future = future1.applyToEitherAsync(future2, (value) -> {
//            System.out.println("result"+Thread.currentThread().getName());
            return value;
        }, executorService);
        System.out.println(future.get());
        executorService.shutdown();
    }
}
